package org.dromara.assets.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.assets.domain.bo.AssetsTokenpairBo;
import org.dromara.assets.domain.vo.AssetsTokenpairVo;
import org.dromara.assets.domain.AssetsTokenpair;
import org.dromara.assets.mapper.AssetsTokenpairMapper;
import org.dromara.assets.service.IAssetsTokenpairService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 交易对Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-06
 */
@Slf4j
@RequiredArgsConstructor
@DS("assets")
@Service
public class AssetsTokenpairServiceImpl implements IAssetsTokenpairService {

    private final AssetsTokenpairMapper baseMapper;

    /**
     * 查询交易对
     *
     * @param id 主键
     * @return 交易对
     */
    @Override
    public AssetsTokenpairVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询交易对列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 交易对分页列表
     */
    @Override
    public TableDataInfo<AssetsTokenpairVo> queryPageList(AssetsTokenpairBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AssetsTokenpair> lqw = buildQueryWrapper(bo);
        Page<AssetsTokenpairVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的交易对列表
     *
     * @param bo 查询条件
     * @return 交易对列表
     */
    @Override
    public List<AssetsTokenpairVo> queryList(AssetsTokenpairBo bo) {
        LambdaQueryWrapper<AssetsTokenpair> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AssetsTokenpair> buildQueryWrapper(AssetsTokenpairBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AssetsTokenpair> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AssetsTokenpair::getId);
        lqw.eq(bo.getPlatform() != null, AssetsTokenpair::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getTokenPair()), AssetsTokenpair::getTokenPair, bo.getTokenPair());
        lqw.eq(bo.getStatus() != null, AssetsTokenpair::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), AssetsTokenpair::getComment, bo.getComment());
        return lqw;
    }

    /**
     * 新增交易对
     *
     * @param bo 交易对
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AssetsTokenpairBo bo) {
        AssetsTokenpair add = MapstructUtils.convert(bo, AssetsTokenpair.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改交易对
     *
     * @param bo 交易对
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AssetsTokenpairBo bo) {
        AssetsTokenpair update = MapstructUtils.convert(bo, AssetsTokenpair.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AssetsTokenpair entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除交易对信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
